package startup;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import presentation.NetworkSetupView;

import businesslogicservice.*;

public class Network {
	private static String filePath = "host";
	public static String host = getHost();
	
	public static BookBLService bookBL;
	public static CustomerBLService customerBL;
	public static OrderBLService orderBL;
	public static PromotionBLService promotionBL;
	public static SalesBLService salesBL;
	public static UserBLService userBL;
	
	
	public static void init() throws MalformedURLException, RemoteException, NotBoundException {
	
			bookBL = (BookBLService) Naming.lookup("rmi://"+host+":8888/bookBL");
			customerBL = (CustomerBLService) Naming.lookup("rmi://"+host+":8888/customerBL");
			orderBL = (OrderBLService) Naming.lookup("rmi://"+host+":8888/orderBL");
			promotionBL = (PromotionBLService) Naming.lookup("rmi://"+host+":8888/promotionBL");
			salesBL = (SalesBLService) Naming.lookup("rmi://"+host+":8888/salesBL");
            userBL = (UserBLService) Naming.lookup("rmi://"+host+":8888/userBL");  
	}
	
	public static String getHost(){
		String s = IOHelper.readFile(filePath);
		if(s==null){
			s = "localHost";
			IOHelper.createFile(filePath, s);
			setHost(s);
		}
		return s;
	}
	
	public static void setHost(String s){
		host = s;
		IOHelper.writeFile(filePath, s);
	}
}
